﻿/**
 * ...
 * @author Innokentiy Shuvalov
 */

package list;

import flash.ui.Mouse;
import gui.hug.HUGScrollButton;
import gui.hug.HUGSprite;
import gui.hug.Utils;
import flash.events.MouseEvent;

class ScrollBar extends HUGSprite{
	
	private static inline var W : UInt = 16;
	private static inline var SLIDER_LENGTH : UInt = 2 * W  + 2;
	private var H : UInt;
	
	private var isOver : Bool;
	private var dragging : Bool;

    private var arrow1 : HUGScrollButton;
    private var arrow2 : HUGScrollButton;
    private var slider : HUGSprite;
	private var sldLength : UInt;
	private var mousePos : Float;
	private var direction : Bool;
	var qnt:UInt;
    
	public function new(x : Float, y : Float, height : UInt) {
		var hArrow;
		super(x, y);
		H = height;
		//	trace(pagesize);
		
		isOver = false;
		dragging = false;
		sldLength = SLIDER_LENGTH;
		
		// Scroll arrows
		arrow1 = new HUGScrollButton( 0, 0, false );
		addChild( arrow1 );
		arrow1.addEventListener( MouseEvent.CLICK, onUpScroll );

		hArrow=arrow1.height;
		arrow2 = new HUGScrollButton( 0, Std.int(H - hArrow), true);
		addChild( arrow2 );
		arrow2.addEventListener( MouseEvent.CLICK, onDownScroll );
		  
		// Slider
		slider = new HUGSprite();
		slider.buttonMode = true;
		slider.x = 0;
		slider.y = W;
		slider.buttonMode = true;
		slider.useHandCursor = true;

		draw();
		 
		slider.addEventListener( MouseEvent.MOUSE_OVER, onSliderOver );
		slider.addEventListener( MouseEvent.MOUSE_OUT, onSliderOut );
		slider.addEventListener( MouseEvent.MOUSE_DOWN, onDrag );
		slider.addEventListener( MouseEvent.MOUSE_UP, onDrop );
		//slider.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
		  
		addChild( slider );
		
		//addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
		//addEventListener( MouseEvent.MOUSE_UP, onDrop );
		addEventListener( MouseEvent.MOUSE_OUT, disableDragging );
    }
	
	static public function getWidth() : UInt {
		return W;
	}
	
	 private function recalcSliderLen()
    {
      var hh:Int = H - 2*16 + 1;
  
       var sliderLen = Math.round (1 * hh) ;/// (maxPos + pageSz) );
        if ( sliderLen > hh ) sliderLen = hh;
        if ( sliderLen < 10 ) sliderLen = 10;
  
      
      slider.y = hh - sliderLen - 1;
	 // redrawSlider();
    }
	
    private function draw(?pos : Float) {
		if ( pos != null ) {
	//		trace(pos + " w - " + W + " h - " + H);
			if ( pos > W && pos < H - W) {
//				trace("Here " + slider.y);
				slider.y = pos;
		//		trace("after Here " + slider.y);
			}
			if ( pos < W ) { slider.y = W; }
			
			
			if ( pos > H - W - sldLength) { slider.y = H - W  - sldLength; }
			
			
		}
		
		//this.graphics.clear();
		//slider.graphics.clear();

		// ScrollBar background
		Utils.rect( this, 0, 0, W, H, 0xFAFAFA, 0xcccccc );
		  
		// Slider
		var c: UInt = (isOver || dragging) ? Utils.SEL_BG_COL : Utils.ARROW_BG_COL;
		var bc: UInt = (isOver || dragging) ? Utils.SEL_BG_BORDER_COL : Utils.BORDER2_COL;
		
		Utils.rect( slider, 0, 0, W, sldLength , c, bc );
		
    }
	
	public function setSliderPosition(firstShownItem : UInt) {
		var quantity = cast(parent, NewList).getQuantity();
		slider.graphics.clear();
		var s : Int = quantity < 8 ? quantity - 1 : 8;
		sldLength = cast(s / quantity * H - W);
		qnt = quantity;
		draw((firstShownItem / quantity) * H);
	}
	
	
	///// EVENT HANDLERS //////////////////////////////////////////////////////////////////////////
	
	public function onUpScroll(e : MouseEvent) {
		cast(parent, NewList).dispatchEvent(new ScrollEvent(ScrollEvent.TWO_UP));
		e.stopPropagation();
	}

	public function onDownScroll(e : MouseEvent) {
		cast(parent, NewList).dispatchEvent(new ScrollEvent(ScrollEvent.TWO_DOWN));
		e.stopPropagation();
	}

	private function onSliderOver(e : MouseEvent) {
		//setChildIndex( slider, numChildren - 1 );
		isOver = true;
		draw();
    }
    
	private function onSliderOut(e : MouseEvent) {
		if (slider.hasEventListener(MouseEvent.MOUSE_MOVE)) {
			slider.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			
		}
		isOver = false;
		draw();
	}
    
    private function onSliderDown(e : MouseEvent) {
		if (isOver) dragging = true;
		e.stopPropagation();
	}

    private function onMouseMove(e : MouseEvent) {
		if (isOver) {
			var t = e.stageY;
			//trace(e.stageY);
			direction = ((mousePos - t) < 0);
			draw(direction ? Math.floor(slider.y + mousePos - t) : Math.floor(slider.y - mousePos + t));
			//trace(e.stageY);
			mousePos = t;
				
			var event = new ScrollEvent(direction ? ScrollEvent.DOWN : ScrollEvent.UP);
			
			cast(parent, NewList).dispatchEvent(event);
		}
		e.stopPropagation();
	}
	
	/*
	private function onMouseMove2(e : MouseEvent) {
		if (isOver) {
			direction = mousePos - e.stageY < 0;
			draw (direction ? slider.y + Math.floor(H / 20) : slider.y - Math.floor(H / 20));
			mousePos = e.stageY;
			
			var event = new ScrollEvent(direction ? ScrollEvent.DOWN : ScrollEvent.UP);
			
			cast(parent, NewList).dispatchEvent(event);
		}
	}
	*/
	private function disableDragging(e : MouseEvent) {
		isOver = false;
		//dragging = false;
	}
	
	private function onDrag( e : MouseEvent ) {
		slider.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		//trace("added");
		//slider.graphics.clear();
		//draw();
	}
	
	private function onDrop (e : MouseEvent) {
//		trace("removing");
		slider.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
	//	trace("removed");
		dragging = false;
		//isOver = false;
		//slider.graphics.clear();
		//draw();
	}
	
	private function stopSlider() {
		
	}
}